import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

// https://webpack.js.org/guides/dependency-management/#requirecontext
const modulesFiles = require.context('./modules', true, /\.js$/)
const modules = modulesFiles.keys().reduce((previousValue, currentValue) => {
  // set './app.js' => 'app'
  const moduleName = currentValue.replace(/^\.\/(.*)\.\w+$/, '$1')
  const value = modulesFiles(currentValue)
  previousValue[moduleName] = value.default
  return previousValue
}, {})

const store = new Vuex.Store({
  modules,
  getters: {
    ui: state => state.app.ui,
    token: state => state.user.token,
    visitedViews: state => state.tagsView.visitedViews,
    cachedViews: state => state.tagsView.cachedViews,
    permission_routes: state => state.permission.routes,
    addRoutes: state => state.permission.addRoutes
  }
})

export default store
